package com.zx.web;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zx.service.UserService;
import jdk.jfr.Frequency;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class UserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        //  登录学生信息管理系统

        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        String username = req.getParameter("username");
        String password = req.getParameter("password");

        Map<String, Object> map = new HashMap<>();

        if(UserService.signIn(username, password)){

            HttpSession session = req.getSession();
            session.setAttribute("username", username);

            map.put("isExist", true);
            ObjectMapper om = new ObjectMapper();
            String s = om.writeValueAsString(map);
            resp.getWriter().print(s);

        }else{
            map.put("isExist", false);
            ObjectMapper om = new ObjectMapper();
            String s = om.writeValueAsString(map);
            resp.getWriter().print(s);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
